//
//  forgotViewController.m
//  B项目
//
//  Created by lanou on 16/8/6.
//  Copyright © 2016年 Work. All rights reserved.
//

#import "forgotViewController.h"
#import "PrefixHeader.pch"
#import <AVOSCloud/AVOSCloud.h>
#import "YGValidator.h"
@interface forgotViewController ()<UITextFieldDelegate>
//手机号
@property(nonatomic,strong)UITextField *userTF;

//验证码密码
@property(nonatomic,strong) UITextField *passWordUserTF;

//新密码
@property(nonatomic,strong)UITextField *NewpassWordUserTF;
//再次输入新密码
@property(nonatomic,strong)UITextField *ReNewpassWordUserTF;
@end

@implementation forgotViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"找回密码";
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftItemAction)];
    self.navigationItem.leftBarButtonItem = leftItem;
    [self setforGet];


}

- (void)leftItemAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setforGet
{
    UIView *userView = [[UIView alloc]initWithFrame:CGRectMake(20, 30, kScreenWidth - 40, 60)];

    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 45, userView.ownWidth, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    UIImageView *userImg = [[UIImageView alloc]initWithFrame:CGRectMake(0,10, 30, 30)];

    userImg.image = [UIImage imageNamed:@"phone.png"];

    self.userTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 10, userView.ownWidth -40, 30)];
    _userTF.placeholder = @"请输入手机号";
    _userTF.font = [UIFont systemFontOfSize:14];
    [_userTF setBorderStyle:(UITextBorderStyleNone)];
    [userView addSubview:_userTF];
    [userView addSubview:userImg];
    [userView addSubview:lineView];
    [self.view addSubview:userView];



    UIView *passWordView = [[UIView alloc]initWithFrame:CGRectMake(20, 90, kScreenWidth - 40, 60)];
    passWordView.userInteractionEnabled = YES;

    UIView *lineViewP = [[UIView alloc]initWithFrame:CGRectMake(0, 45, userView.ownWidth, 1)];
    lineViewP.backgroundColor = [UIColor grayColor];
    UIImageView *passWordImg = [[UIImageView alloc]initWithFrame:CGRectMake(0,10, 30, 30)];
    passWordImg.image = [UIImage imageNamed:@"yanzheng.png"];

   self.passWordUserTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 10, passWordView.ownWidth -120, 30)];
    _passWordUserTF.placeholder = @"请输入验证码";
    _passWordUserTF.font = [UIFont systemFontOfSize:14];
    [_passWordUserTF setBorderStyle:(UITextBorderStyleNone)];

    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(passWordImg.ownWidth + _passWordUserTF.ownWidth, 10, 100, 30);
    [button setTitle:@"获取验证码" forState:(UIControlStateNormal)];
    button.titleLabel.font = [UIFont systemFontOfSize:12];
    [button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(btAction) forControlEvents:(UIControlEventTouchUpInside)];

    [passWordView addSubview:lineViewP];
    [passWordView addSubview:_passWordUserTF];
    [passWordView addSubview:passWordImg];
    [passWordView addSubview:button];
    [self.view addSubview:passWordView];

//新密码
    UIView *newPassView = [[UIView alloc]initWithFrame:CGRectMake(20, 150, kScreenWidth - 40, 60)];
    UIImageView *newImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 30, 30)];
    newImg.image = [UIImage imageNamed:@"password.png"];

    self.NewpassWordUserTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 10, newPassView.ownWidth -120, 30)];
    _NewpassWordUserTF.placeholder = @"请输入新密码";
    _NewpassWordUserTF.font = [UIFont systemFontOfSize:14];
    [_NewpassWordUserTF setBorderStyle:(UITextBorderStyleNone)];

    UIView *newlineViewP = [[UIView alloc]initWithFrame:CGRectMake(0, 45, newPassView.ownWidth, 1)];
    newlineViewP.backgroundColor = [UIColor grayColor];

    [newPassView addSubview:newImg];
    [newPassView addSubview:_NewpassWordUserTF];
    [newPassView addSubview:newlineViewP];
    [self.view addSubview:newPassView];

//再次输入新密码
    UIView *ReNewPassView = [[UIView alloc]initWithFrame:CGRectMake(20, 210, kScreenWidth - 40, 60)];
    UIImageView *ReNewImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 30, 30)];
    ReNewImg.image = [UIImage imageNamed:@"password.png"];

    self.ReNewpassWordUserTF = [[UITextField alloc]initWithFrame:CGRectMake(40, 10, ReNewPassView.ownWidth -120, 30)];
    self.ReNewpassWordUserTF.placeholder = @"请输入新密码";
    self.ReNewpassWordUserTF.font = [UIFont systemFontOfSize:14];
    [self.ReNewpassWordUserTF setBorderStyle:(UITextBorderStyleNone)];

    UIView *RenewlineViewP = [[UIView alloc]initWithFrame:CGRectMake(0, 45, ReNewPassView.ownWidth, 1)];
    RenewlineViewP.backgroundColor = [UIColor grayColor];

    [ReNewPassView addSubview:ReNewImg];
    [ReNewPassView addSubview:self.ReNewpassWordUserTF];
    [ReNewPassView addSubview:RenewlineViewP];
    [self.view addSubview:ReNewPassView];


    //完成

    UIImageView *completeImg = [[UIImageView alloc]initWithFrame:CGRectMake(20, 300, kScreenWidth - 40, 30)];
    completeImg.userInteractionEnabled = YES;
    completeImg.image = [UIImage imageNamed:@"login_tip_button@2x.png"];
    UIButton *completeBT = [[UIButton alloc]initWithFrame:CGRectMake((completeImg.ownWidth -60)/2, 0, 60, 30)];
    [completeBT setTitle:@"完成" forState:(UIControlStateNormal)];
    [completeBT setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [completeBT addTarget:self action:@selector(loaginBTAction) forControlEvents:(UIControlEventTouchUpInside)];
    [completeImg addSubview:completeBT];
    [self.view addSubview:completeImg];

}


//获取验证码点击事件
- (void)btAction
{
    [AVUser requestPasswordResetWithPhoneNumber:self.userTF.text block:^(BOOL succeeded, NSError *error) {
        if (succeeded) {


        } else {
            
        }
    }];
    
}

//点击完成按钮
- (void)loaginBTAction
{
    if ([_NewpassWordUserTF.text isEqualToString:_ReNewpassWordUserTF.text]) {
        [AVUser resetPasswordWithSmsCode:_passWordUserTF.text newPassword:self.NewpassWordUserTF.text block:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                UIAlertController *alerC = [UIAlertController alertControllerWithTitle:@"恭喜，成功找回密码" message:@"是否返回登陆页面，重新登陆" preferredStyle:(UIAlertControllerStyleAlert)];
                UIAlertAction *alerActionTrue = [UIAlertAction actionWithTitle:@"是" style:(UIAlertActionStyleDefault) handler:nil];
                UIAlertAction *alerActionFalse = [UIAlertAction actionWithTitle:@"否" style:(UIAlertActionStyleDefault) handler:nil];
                [self presentViewController:alerC animated:YES completion:nil];
                [alerC addAction:alerActionTrue];
                [alerC addAction:alerActionFalse];

            } else {
                UIAlertController *alerC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请重新核对后输入" preferredStyle:(UIAlertControllerStyleAlert)];
                UIAlertAction *alerActionTrue = [UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleDefault) handler:nil];

                [self presentViewController:alerC animated:YES completion:nil];
                [alerC addAction:alerActionTrue];

            }
        }];
        


    }

}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{

    NSMutableString *currentPassWordStr = [NSMutableString stringWithString:_userTF.text];
    [currentPassWordStr replaceCharactersInRange:range withString:string];
    [YGValidator validateText:currentPassWordStr regular:@"\\w{6,16}"@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$" textField:_userTF correctImage:@"true" errorImage:@"error"];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
